function _typeof(obj) {
  '@babel/helpers - typeof';
  return (
    (_typeof =
      'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
        ? function (obj) {
            return typeof obj;
          }
        : function (obj) {
            return obj &&
              'function' == typeof Symbol &&
              obj.constructor === Symbol &&
              obj !== Symbol.prototype
              ? 'symbol'
              : typeof obj;
          }),
    _typeof(obj)
  );
}
var _excluded = ['required', 'className', 'children', 'rules', 'when', 'dynamicProps'];
function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _slicedToArray(arr, i) {
  return (
    _arrayWithHoles(arr) ||
    _iterableToArrayLimit(arr, i) ||
    _unsupportedIterableToArray(arr, i) ||
    _nonIterableRest()
  );
}
function _nonIterableRest() {
  throw new TypeError(
    'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _iterableToArrayLimit(arr, i) {
  var _i =
    null == arr
      ? null
      : ('undefined' != typeof Symbol && arr[Symbol.iterator]) || arr['@@iterator'];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else
        for (
          ;
          !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i);
          _n = !0
        );
    } catch (err) {
      (_d = !0), (_e = err);
    } finally {
      try {
        if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly &&
      (symbols = symbols.filter(function (sym) {
        return Object.getOwnPropertyDescriptor(object, sym).enumerable;
      })),
      keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2
      ? ownKeys(Object(source), !0).forEach(function (key) {
          _defineProperty(target, key, source[key]);
        })
      : Object.getOwnPropertyDescriptors
      ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
      : ownKeys(Object(source)).forEach(function (key) {
          Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
  }
  return target;
}
function _defineProperty(obj, key, value) {
  key = _toPropertyKey(key);
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true,
    });
  } else {
    obj[key] = value;
  }
  return obj;
}
function _toPropertyKey(arg) {
  var key = _toPrimitive(arg, 'string');
  return _typeof(key) === 'symbol' ? key : String(key);
}
function _toPrimitive(input, hint) {
  if (_typeof(input) !== 'object' || input === null) return input;
  var prim = input[Symbol.toPrimitive];
  if (prim !== undefined) {
    var res = prim.call(input, hint || 'default');
    if (_typeof(res) !== 'object') return res;
    throw new TypeError('@@toPrimitive must return a primitive value.');
  }
  return (hint === 'string' ? String : Number)(input);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Form } from 'antd';
import classnames from 'classnames';
import React, { useMemo } from 'react';
import FieldContext from '../../_utils/contexts/FieldContext';
import useField from '../../_utils/hooks/useField';
import When from '../When';
import './index.less';
/**
 * 表单容器
 * 扩展 when 字段用于表单联动
 * 扩展 dynamicProps 字段用于获取动态属性
 */
export default function FormItem(_ref) {
  var _ref$required = _ref.required,
    required = _ref$required === void 0 ? true : _ref$required,
    className = _ref.className,
    children = _ref.children,
    _ref$rules = _ref.rules,
    rules = _ref$rules === void 0 ? [] : _ref$rules,
    when = _ref.when,
    dynamicProps = _ref.dynamicProps,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useField = useField({
      required: required,
      label: rest.label,
      rules: rules,
    }),
    rulesWithRequired = _useField.rulesWithRequired;
  var props = useMemo(
    function () {
      return _objectSpread(
        _objectSpread(
          {
            className: classnames('szzj-form-item', className),
            required: required,
            validateFirst: true,
          },
          rest,
        ),
        {},
        {
          rules: rulesWithRequired,
        },
      );
    },
    [className, required, rest, rulesWithRequired],
  );
  if (when || dynamicProps) {
    var _ref2 = when !== null && when !== void 0 ? when : [],
      _ref3 = _slicedToArray(_ref2, 2),
      _ref3$ = _ref3[0],
      predicate =
        _ref3$ === void 0
          ? function () {
              return true;
            }
          : _ref3$,
      _ref3$2 = _ref3[1],
      dependencies = _ref3$2 === void 0 ? [] : _ref3$2;
    var _ref4 = dynamicProps !== null && dynamicProps !== void 0 ? dynamicProps : [],
      _ref5 = _slicedToArray(_ref4, 2),
      _ref5$ = _ref5[0],
      getDynamicProps = _ref5$ === void 0 ? function () {} : _ref5$,
      _ref5$2 = _ref5[1],
      getDynamicPropsDeps = _ref5$2 === void 0 ? [] : _ref5$2;
    return (
      /*#__PURE__*/
      // @ts-ignore
      React.createElement(
        When,
        {
          when: [
            predicate,
            [].concat(_toConsumableArray(dependencies), _toConsumableArray(getDynamicPropsDeps)),
          ],
        },
        function (form) {
          var finalProps = _objectSpread(_objectSpread({}, props), getDynamicProps(form, props));
          return /*#__PURE__*/ React.createElement(
            FieldContext.Provider,
            {
              value: finalProps,
            },
            /*#__PURE__*/ React.createElement(Form.Item, finalProps, children),
          );
        },
      )
    );
  }
  return /*#__PURE__*/ React.createElement(
    FieldContext.Provider,
    {
      value: props,
    },
    /*#__PURE__*/ React.createElement(Form.Item, props, children),
  );
}
